Micron Document
🎖️GitЯра🎖️


Displaying Raw • Download

feature/firmware/src/jvmAndroidMain/kotlin/org/meshtastic/feature/firmware/FirmwareDownload.kt 65a1f5ce4d0e4b8eafcd0bbfccafd41dc6ee888a (65a1f5ce) Text, 3.04 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.feature.firmware

Tff7b72import T7ee787io.ktor.client.statement.HttpResponse
Tff7b72import T7ee787io.ktor.client.statement.bodyAsChannel
Tff7b72import T7ee787io.ktor.http.contentLength
Tff7b72import T7ee787io.ktor.utils.io.jvm.javaio.toInputStream
Tff7b72import T7ee787kotlinx.coroutines.ensureActive
Tff7b72import T7ee787java.io.File
Tff7b72import T7ee787java.io.FileOutputStream
Tff7b72import T7ee787java.io.IOException
Tff7b72import T7ee787kotlin.coroutines.coroutineContext

Tff7b72private Tff7b72const Tff7b72val Te6edf3DOWNLOAD_BUFFER_SIZE Tff7b72= T79c0ff8T79c0ff1T79c0ff9T79c0ff2

T8b949e/**
* Streams [response]'s body into [targetFile], reporting fractional progress via [onProgress] as bytes arrive.
*
* Shared by the Android and desktop JVM [FirmwareFileHandler] implementations so the two byte-copy loops (and their
* cancellation/completeness checks) cannot drift apart — see [extractZipEntriesBounded] for the same rationale on the
* zip-extraction side. Streams incrementally rather than buffering the whole body: ktor's built-in `onDownload`
* progress hook pairs naturally with `HttpResponse.body(): ByteArray`, but that holds an entire firmware image — tens
* of MB — in memory (twice over, briefly), which isn't worth it just to drop a manual loop on a download this size.
*
* @throws IOException if the transfer is shorter than the response's declared `Content-Length`.
* @throws kotlinx.coroutines.CancellationException if the calling coroutine is cancelled mid-transfer.
*/
Tff7b72internal Tff7b72suspend Tff7b72fun Td2a8ffdownloadResponseToFileTb4b4b4(Te6edf3responseTb4b4b4: Te6edf3HttpResponseTb4b4b4, Te6edf3targetFileTb4b4b4: Te6edf3FileTb4b4b4, Te6edf3onProgressTb4b4b4: Tb4b4b4(Tffa657FloatTb4b4b4) Tff7b72-Tff7b72> Tffa657UnitTb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3body Tff7b72= Te6edf3responseTb4b4b4.Te6edf3bodyAsChannelTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3contentLength Tff7b72= Te6edf3responseTb4b4b4.Te6edf3contentLengthTb4b4b4(Tb4b4b4) Tff7b72?: Tff7b72-T79c0ff1L

Te6edf3bodyTb4b4b4.Te6edf3toInputStreamTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3use Tb4b4b4{ Te6edf3input Tff7b72-Tff7b72>
Te6edf3FileOutputStreamTb4b4b4(Te6edf3targetFileTb4b4b4)Tb4b4b4.Te6edf3use Tb4b4b4{ Te6edf3output Tff7b72-Tff7b72>
Tff7b72val Te6edf3buffer Tff7b72= Te6edf3ByteArrayTb4b4b4(Te6edf3DOWNLOAD_BUFFER_SIZETb4b4b4)
Tff7b72var Te6edf3bytesReadTb4b4b4: Tffa657Int
Tff7b72var Te6edf3totalBytesRead Tff7b72= T79c0ff0L

Tff7b72while Tb4b4b4(Te6edf3inputTb4b4b4.Te6edf3readTb4b4b4(Te6edf3bufferTb4b4b4)Tb4b4b4.Te6edf3also Tb4b4b4{ Te6edf3bytesRead Tff7b72= Tffa657it Tb4b4b4} Tff7b72!Tff7b72= Tff7b72-T79c0ff1Tb4b4b4) Tb4b4b4{
Te6edf3coroutineContextTb4b4b4.Te6edf3ensureActiveTb4b4b4(Tb4b4b4)

Te6edf3outputTb4b4b4.Te6edf3writeTb4b4b4(Te6edf3bufferTb4b4b4, T79c0ff0Tb4b4b4, Te6edf3bytesReadTb4b4b4)
Te6edf3totalBytesRead Tff7b72+Tff7b72= Te6edf3bytesRead

Tff7b72if Tb4b4b4(Te6edf3contentLength Tff7b72> T79c0ff0Tb4b4b4) Tb4b4b4{
Te6edf3onProgressTb4b4b4(Te6edf3totalBytesReadTb4b4b4.Te6edf3toFloatTb4b4b4(Tb4b4b4) Tff7b72/ Te6edf3contentLengthTb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tff7b72if Tb4b4b4(Te6edf3contentLength Tff7b72!Tff7b72= Tff7b72-T79c0ff1L Tff7b72&Tff7b72& Te6edf3totalBytesRead Tff7b72!Tff7b72= Te6edf3contentLengthTb4b4b4) Tb4b4b4{
Tff7b72throw Te6edf3IOExceptionTb4b4b4(Ta5d6ff"Ta5d6ffIncomplete download: expected Tffd700$Te6edf3contentLengthTa5d6ff bytes, got Tffd700$Te6edf3totalBytesReadTa5d6ff"Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.0 - Generated in 0.06s